<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" :rules="dataRule" ref="inputForm" @keyup.enter.native="doSubmit()"
             label-width="120px" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'" @submit.native.prevent>
      <el-row :gutter="15">

        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="inputForm.name" maxlength="50" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="company.id" label="公司">
            <SelectTree
              ref="companyTree"
              :props="{
                  value: 'id',
                  label: 'name',
                  children: 'childNodes'
                }"
              :url="`/sys/office/treeData?type=1&&extId=${this.inputForm.id}`"
              :value="inputForm.company.id"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {inputForm.company.id=value}"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="手机" prop="mobile" :rules="[{validator:validator.isMobile, trigger:'blur'}]">
            <el-input v-model="inputForm.mobile" maxlength="50" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="office.id" label="部门">
            <SelectTree
              ref="officeTree"
              :props="{
                  value: 'id',
                  label: 'name',
                  children: 'childNodes'
                }"
              :url="`/sys/office/treeData?type=2&&extId=${this.inputForm.id}`"
              :value="inputForm.office.id"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {inputForm.office.id=value}"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="登录名" prop="loginName">
            <el-input v-model="inputForm.loginName" maxlength="50" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工号" prop="no" :rules="[{required: true, message:'工号不能为空', trigger:'blur'}]">
            <el-input v-model="inputForm.no" maxlength="50" placeholder=""></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="密码:" prop="newPassword" :rules="inputForm.id && inputForm.newPassword=='' ?[]:[{required: true, message:'密码不能为空', trigger:'blur'},{validator: validator.isPwd, trigger:'blur'}]">
            <el-input v-model="inputForm.newPassword" maxlength="50" placeholder="若不修改，请留空" show-password></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="确认密码" prop="confirmNewPassword" :rules="inputForm.id && inputForm.newPassword==''?[]:[{required: true, message:'确认密码不能为空', trigger:'blur'},{validator: validatePass2, trigger: 'blur'}]">
            <el-input v-model="inputForm.confirmNewPassword" maxlength="50" placeholder="" show-password></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="邮箱" prop="email" :rules="[{type:'email', message:'请输入正确的邮箱地址', trigger:'blur'}]">
            <el-input v-model="inputForm.email" maxlength="50" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="phone" :rules="[{validator:validator.isPhone, trigger:'blur'}]">
            <el-input v-model="inputForm.phone" maxlength="50" placeholder=""></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="允许登录" prop="loginFlag">
            <el-radio-group v-model="inputForm.loginFlag">
              <el-radio v-for="item in this.$dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色" prop="roleIdList">
            <el-select
              v-model="inputForm.roleIdList"
              multiple
              filterable
              placeholder="请选择角色"
              style="width: 100%">
              <el-option
                v-for="role in roleList"
                :key="role.id"
                :label="role.name"
                :value="role.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="负责区域" prop="areaIds">
            <el-select
              v-model="inputForm.areaIds"
              multiple
              filterable
              placeholder="请选择负责区域"
              style="width: 100%">
              <el-option
                v-for="item in areaList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item prop="photo" label="头像">
            <el-upload
              class="avatar-uploader"
              :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=photo`"
              :headers="{token: $cookie.get('token')}"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              :show-file-list="false">
              <img v-if="inputForm.photo" :src="inputForm.photo" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input type="textarea" v-model="inputForm.remarks" maxlength="200" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SelectTree from '@/components/treeSelect/treeSelect.vue'
export default {
  data () {
    return {
      visible: false,
      loading: false,
      title: '',
      method: '',
      roleList: [],
      areaList: [],
      areaIds: [],
      inputForm: {
        id: '',
        company: { id: '', name: '' },
        office: { id: '', name: '' },
        roleIdList: [],
        loginName: '',
        no: '',
        name: '',
        email: '',
        phone: '',
        mobile: '',
        loginIp: '',
        loginDate: '',
        loginFlag: '1',
        photo: '',
        qrCode: '',
        oldLoginName: '',
        newPassword: '',
        confirmNewPassword: '',
        sign: '',
        oldLoginIp: '',
        oldLoginDate: '',
        remarks: ''
      },
      dataRule: {
        'company.id': [{required: true, message: '公司不能为空', trigger: 'blur'}],
        'office.id': [{required: true, message: '部门不能为空', trigger: 'blur'}],
        name: [{required: true, message: '姓名不能为空', trigger: 'blur'}],
        loginName: [{required: true, message: '登录名不能为空', trigger: 'blur'}],
        roleIdList: [{required: true, message: '角色不能为空', trigger: 'blur'}]
      }
    }
  },
  components: {
    SelectTree
  },
  methods: {
    recover(form, source) {
      if (source && typeof source === 'object') {
        Object.keys(form).forEach(key => {
          if (source[key] !== undefined) {
            if (typeof form[key] === 'object' && form[key] !== null) {
              form[key] = this.recover(form[key], source[key])
            } else {
              form[key] = source[key]
            }
          }
        })
      }
      return form
    },

    validatePass2(rule, value, callback){
      if (value !== this.inputForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    },

    init (method, id) {
      this.method = method
      this.inputForm.id = id

      if (method === 'add') {
        this.title = `新建用户`
      } else if (method === 'edit') {
        this.title = '修改用户'
      } else if (method === 'view') {
        this.title = '查看用户'
      }

      this.visible = true

      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        this.inputForm.oldLoginName = ''

        this.$http({
          url: `/sys/role/list?pageNo=1&pageSize=-1`,
          method: 'get'
        }).then(({ data }) => {
          this.roleList = data.page.list
        })

        this.$http({
          url: `/lora/area/jfLoraArea/list`,
          method: 'get'
        }).then(({ data }) => {
          this.areaList = data.list || []

          if (method === 'edit' || method === 'view') {
            this.loadUserData()
          }
        })

        if (method === 'edit' || method === 'view') {
          if (this.areaList && this.areaList.length > 0) {
            this.loadUserData()
          }
        }
      })
    },

    loadUserData () {
      this.$http({
        url: `/sys/user/queryById?id=${this.inputForm.id}`,
        method: 'get'
      }).then(({ data }) => {
        this.inputForm = this.recover(this.inputForm, data.user)
        this.inputForm.oldLoginName = this.inputForm.loginName

        if (data.user.areaIds) {
          this.inputForm.areaIds = data.user.areaIds.split(',').map(id => id.trim())
        } else {
          this.inputForm.areaIds = []
        }

        if (data.user.roleIdList && data.user.roleIdList.length > 0) {
          this.inputForm.roleIdList = data.user.roleIdList[0].split(',').map(id => id.trim())
        } else {
          this.inputForm.roleIdList = []
        }
      })
    },

    handleAvatarSuccess (res, file) {
      this.inputForm.photo = res.url
    },

    beforeAvatarUpload (file) {
      const isJPG = file.type.indexOf('image/') >= 0
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像只能是图片格式!')
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
        return false
      }
      return true
    },

    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/sys/user/save`,
            method: 'post',
            headers: {arrayFormat: 'repeat'},
            data: this.inputForm
          }).then(({data}) => {
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
